package com.neusoft.shry.dao;

import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.neusoft.shry.dto.PositionDto;
import com.zero.basic.dao.DefaultBaseDao;
import com.zero.basic.dao.SqlListBaseDao;
import com.zero.basic.dao.SqlPagingListBaseDao;
import com.zero.basic.dao.SqlQueryBaseDao;
import com.zero.basic.dao.SqlUpdateBaseDao;
import com.zero.basic.exception.SmsDaoException;
import com.zero.basic.model.Pager;
import com.zero.mms.model.Dep;
import com.zero.mms.model.Employee;
import com.zero.mms.model.Position;
import com.zero.mms.model.SalaryItem;
import com.zero.youxiao.dao.SalaryItemDao;

@Repository("positionDao")
@Transactional(rollbackFor = Exception.class)
public class PositionDaoImp extends DefaultBaseDao<Position> implements PositionDao {

	@Inject
	private SqlQueryBaseDao<?> sqlQueryBaseDao;
	
	@Inject
	private SqlListBaseDao<?> sqlListBaseDao;
	
	@Inject
	private SqlUpdateBaseDao<?> sqlUpdateBaseDao;
	
	@Inject
	private SqlPagingListBaseDao<?> sqlPagingListBaseDao;
	
	//get all positions
	@Override
	public List<Position> getAllPositions() throws SmsDaoException {
		try{
			String sql = "select * from t_position";
			List<Position> positionList = sqlListBaseDao.listBySql(sql,Position.class,true,true);
			return positionList;
		}catch(Exception e){
			e.printStackTrace();
			throw new SmsDaoException();
		}
	}

	//get a position by id
	@Override
	public Position getPositionById(Integer position_id) throws SmsDaoException {
		try{
			String sql = "select * from t_position where position_id = ?";
			Position position = (Position) sqlQueryBaseDao.queryBySql(sql,position_id,Position.class,true,true);
			return position;
		}catch(Exception e){
			e.printStackTrace();
			throw new SmsDaoException();
		}
		
	}

	//get positions by conditions
	@Override
	public Pager<PositionDto> getPositionsByConditions(Integer dep_id,String dep_name,Integer position_id,String position_name,String position_type) throws SmsDaoException {
		try{
			String sql = "select p.position_id as positionId,p.position_name as positionName,d.dep_name as depName,p.position_type as positionType,p.position_max as positionMax "
					+ "from t_position p,t_dep d where p.dep_id = d.dep_id";
			System.out.println(dep_name);
			if (dep_id!=null) {
				sql += " and p.dep_id ='" + dep_id +"'";
			}
			if (!"".equals(dep_name)&&!"*".equals(dep_name)&&dep_name!=null) {
				sql += " and d.dep_name like '%" + dep_name +"%'";
			}
			if (position_id!=null) {
				sql += " and p.position_id = '" + position_id +"'";
			}
			if (!"".equals(position_name)&&!"*".equals(position_name)&&position_name!=null) {
				sql += " and p.position_name like '%" + position_name +"%'";
			}
			if (!"".equals(position_type)&&!"*".equals(position_type)&&position_type!=null) {
				sql += " and p.position_type ='" + position_type +"'";
			}
			System.out.println(sql);
			Pager<PositionDto> pager = sqlPagingListBaseDao.findBySql(sql, PositionDto.class, false);
			return pager;
		}catch(Exception e){
			e.printStackTrace();
			throw new SmsDaoException();
		}
	}

	//update a position
	@Override
	public void updatePosition(Position position) throws SmsDaoException {
		try {
			String sql = "update t_position set position_name = ? , position_type = ? , dep_id = ? , position_max = ? where position_id = ?";
			sqlUpdateBaseDao.updateBySql(sql,new Object[]{position.getPositionName(),position.getPositionType(),position.getDepId(),position.getPositionMax(),position.getPositionId()});
		} catch (Exception e) {
			e.printStackTrace();
			throw new SmsDaoException();
		}
		
	}

	//view employees in a position
	@Override
	public List<Employee> viewEmpInPosition(Integer position_id)
			throws SmsDaoException {
		try{
			String sql = "select * from t_emp e where e.position_id = ?";
			List<Employee> empList = sqlListBaseDao.listBySql(sql, position_id,Employee.class,true,true);
			return empList;
		}catch(Exception e){
			e.printStackTrace();
			throw new SmsDaoException();
		}
	}

	//get dep_name by position_id in a position
	@Override
	public String getDepNameByPositionId(Integer position_id) throws SmsDaoException {
		try{
			String sql = "select d.dep_name from t_position p,t_dep d where p.dep_id = d.dep_id and p.position_id = ?";
			String depName = (String) sqlQueryBaseDao.queryBySql(sql, position_id,Position.class,true,false);
			return depName;
		}catch(Exception e){
			e.printStackTrace();
			throw new SmsDaoException();
		}
	}

	//get all deps
	@Override
	public List<Dep> getAllDeps() throws SmsDaoException {
		try{
			String sql = "select * from t_dep";
			List<Dep> deps = sqlListBaseDao.listBySql(sql, Dep.class, true, true);
			return deps;
		}catch(Exception e){
			e.printStackTrace();
			throw new SmsDaoException();
		}
	}
}
